package com.hongye.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hongye.common.entity.*;
import com.hongye.common.exception.ServiceException;
import com.hongye.common.response.ResponseResult;
import com.hongye.customer.mapper.CustomerMapper;
import com.hongye.customer.service.CustomerService;
import com.hongye.customer.vo.CustomerVo;
import com.hongye.customer.vo.PublishQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ResponseResult slideList() {
        List<SlideshowEntity> slideshowEntityList =  customerMapper.slideList();
        if (CollectionUtils.isEmpty(slideshowEntityList)){
            throw new ServiceException("400","没有正在热卖的房源信息");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("slideshowEntityList",slideshowEntityList);
        return new ResponseResult(402,"轮播图信息",map);
    }

    @Override
    public ResponseResult buyHouse(int isNewHouse) {
        List<HouseEntity> houseEntityList = customerMapper.houseList(isNewHouse);
        if (CollectionUtils.isEmpty(houseEntityList)){
            throw new ServiceException("400","没有正在热卖的房源信息");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("houseEntityList",houseEntityList);
        return new ResponseResult(402,"正在出售的房产信息",map);
    }

    @Override
    public ResponseResult rentHouse() {
        List<RentHouseEntity> rentHouseEntityList = customerMapper.rentHouse();
        if (CollectionUtils.isEmpty(rentHouseEntityList)){
            throw new ServiceException("400","没有正在热卖的房源信息");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("rentHouseEntityList",rentHouseEntityList);
        return new ResponseResult(402,"租房信息",map);
    }

    @Override
    public ResponseResult demandHouse(int publish_type_id, int house_type_id, int city_id) {
        List<PublishQueryVo> vos = customerMapper.demandHouse(publish_type_id, house_type_id, city_id);
        HashMap<String, Object> map = new HashMap<>();
        if (CollectionUtils.isEmpty(vos)){
            throw new ServiceException("400","当前没有找到合适的房源信息!");
        }
        map.put("vos",vos);
        return new ResponseResult(402,"房源信息",map);
    }

    @Override
    public ResponseResult informationList() {
        List<InformationEntity> informationEntityList = customerMapper.informationList();
        if (CollectionUtils.isEmpty(informationEntityList)){
            throw new ServiceException("400","当前没有找到合适的资讯信息!");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("informationEntityList",informationEntityList);
        return new ResponseResult(402,"资讯信息",map);
    }

    @Override
    public ResponseResult houseRing() {
        List<StoreyEntity> storeyEntityList = customerMapper.houseRing();
        if (CollectionUtils.isEmpty(storeyEntityList)){
            throw new ServiceException("400","当前没有找到合适的楼市圈信息!");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("storeyEntityList",storeyEntityList);
        return new ResponseResult(402,"楼市圈信息",map);
    }

    @Override
    public ResponseResult brokerList() {
        List<BrokerEntity> brokerEntityList = customerMapper.brokerList();
        if (CollectionUtils.isEmpty(brokerEntityList)){
            throw new ServiceException("400","当前没有找到合适的经纪人信息!");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("brokerEntityList",brokerEntityList);
        return new ResponseResult(402,"经纪人信息",map);
    }

    @Override
    public ResponseResult groupPurchase() {
        List<GroupEntity> groupEntityList = customerMapper.groupPurchase();
        if (CollectionUtils.isEmpty(groupEntityList)){
            throw new ServiceException("400","当前没有找到合适的拼团!");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("groupEntityList",groupEntityList);
        return new ResponseResult(402,"团购看房列表",map);
    }

    @Override
    public ResponseResult homePage() {
        CustomerVo customerVo = new CustomerVo();
        customerVo.setSlideShowEntityList(customerMapper.slideList());
        customerVo.setInformationEntityList(customerMapper.informationList());
        customerVo.setHouseEntityList(customerMapper.houseList(1));
        customerVo.setBrokerEntityList(customerMapper.brokerList());
        customerVo.setSecondHouseEntityList(customerMapper.houseList(2));
        HashMap<String, Object> map = new HashMap<>();
        map.put("customerVo",customerVo);
        return ResponseResult.ok(402,"首页",map);
    }

    /**
     * 发布信息
     * @param publishEntity
     * @return
     */
    @Transactional
    @Override
    public String buyHouseInsert(PublishEntity publishEntity) {
        if (StringUtils.isEmpty(publishEntity.getSmallCommunity())){
            throw new ServiceException("400","小区不能为空");
        }
        if (StringUtils.isEmpty(publishEntity.getHouseTypeId())){
            throw new ServiceException("400","请先选择房屋类型");
        }
        if (StringUtils.isEmpty(publishEntity.getProvinceId())){
            throw new ServiceException("400","请先选择省份");
        }
        if (StringUtils.isEmpty(publishEntity.getCityId())){
            throw new ServiceException("400","请先选择市");
        }
        if (StringUtils.isEmpty(publishEntity.getCountryId())){
            throw new ServiceException("400","请先选择县");
        }
        if (StringUtils.isEmpty(publishEntity.getHouseType())){
            throw new ServiceException("400","请选择房屋类型");
        }
        if (StringUtils.isEmpty(publishEntity.getPrice())){
            throw new ServiceException("400","请输入求租房屋价格");
        }
        if(StringUtils.isEmpty(publishEntity.getPublishTypeId())){
            throw new ServiceException("400","请选择发布类型");
        }
        CustomerEntity customerEntity = customerMapper.findById(1);
        publishEntity.setCustomerId(1);
        publishEntity.setStatus(1);//发布中
        //1.添加一条发布记录
        customerMapper.buyHouseInsert(publishEntity);
        //2.添加一条订单记录
        OrderEntity order = new OrderEntity();
        String timeId = IdWorker.getTimeId();
        order.setId(timeId);
        order.setCreateTime(new Date());
        order.setCustomerId(Long.parseLong("1"));
        order.setCustomerName(customerEntity.getCustomerName());
        order.setPayment(new BigDecimal(10)); //支付10元
        order.setPhone(customerEntity.getCustomerPhone());
        order.setPublishTypeId(publishEntity.getPublishTypeId());
        order.setStatus(1);//未付款
        customerMapper.addOrder(order);
        //3.添加一条流水记录
        PayLogEntity payLogEntity = new PayLogEntity();
        payLogEntity.setOutTradeNo("tb"+System.currentTimeMillis());
        payLogEntity.setCreateTime(new Date());
        payLogEntity.setTotalFee(10L); //支付10元
        payLogEntity.setUserId(customerEntity.getId().toString());
        payLogEntity.setOrderList(timeId);
        customerMapper.addPayLog(payLogEntity);
        return timeId;
    }

    @Override
    public CustomerEntity findById(int id) {
        CustomerEntity customerEntity = customerMapper.findById(id);
        return customerEntity;
    }

    @Override
    public ResponseResult apply(Integer customerId, Integer groupId) {
        GroupEntity entity = customerMapper.apply(groupId);
        if (ObjectUtils.isEmpty(entity)){
            throw new ServiceException("201","该楼盘不存在");
        }
        if (entity.getGroupStatus()==1){
            throw new ServiceException("201","已开团，报名时间已过");
        }
        long time = entity.getEndTime().getTime();
        long time1 = new Date().getTime();
        if (time<time1){
            throw new ServiceException("201","团购已结束，请另选楼盘");
        }
        GroupCustomerEntity groupCustomerEntity = customerMapper.findByIds(groupId,customerId);
        if (ObjectUtil.isNotEmpty(groupCustomerEntity)){
            throw new ServiceException("201","已报过名，不可重复报名");
        }
        if (entity.getGroupNum()==null){
            entity.setGroupNum(1);
            customerMapper.update(groupId,entity.getGroupNum());
            customerMapper.addGroupCustomer(customerId,groupId);
            return new ResponseResult(200,"报名成功");
        }

        entity.setGroupNum(entity.getGroupNum()+1);
        customerMapper.update(groupId,entity.getGroupNum());
        customerMapper.addGroupCustomer(customerId,groupId);
        return new ResponseResult(200,"报名成功");
    }
}
